package com.gu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gu.entity.EduChapter;
import com.gu.entity.EduVideo;
import com.gu.entity.vo.ChapterVo;
import com.gu.entity.vo.VideoVo;
import com.boot.exception.DiyException;
import com.gu.service.EduChapterService;
import com.gu.mapper.EduChapterMapper;
import com.gu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter>
        implements EduChapterService {

    @Resource
    private EduVideoService eduVideoService;

    @Override
    @Transactional
    public List<ChapterVo> getChapters(String courseId) {

        //创建返回给前端 相对应 级联 结构的集合
        List<ChapterVo> ChapterVoList = new ArrayList<>();
        //当值为 "" 的时候即是查询所有章节信息；
        if (courseId != null) {

            QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();

            chapterWrapper.eq("course_id", courseId);
            chapterWrapper.orderByAsc("sort");
            List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);
            //遍历章节信息：
            chapterList.forEach(chapter -> {
                //videoVo list
                List<VideoVo> videoVoList = new ArrayList<>();
                ChapterVo chapterVo = new ChapterVo();

                String chapterId = chapter.getId();
                BeanUtils.copyProperties(chapter, chapterVo);

                QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
                //小节升序处理
                videoWrapper.eq("chapter_id", chapterId);
                videoWrapper.orderByAsc("sort");
                List<EduVideo> videoUnderChapterList = eduVideoService.list(videoWrapper);
                //遍历相对应章节下的 小结 集合
                videoUnderChapterList.forEach(videoUnderChapter -> {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(videoUnderChapter, videoVo);
                    videoVoList.add(videoVo);
                });
                chapterVo.setChildren(videoVoList);
                ChapterVoList.add(chapterVo);
            });
        } else {
            throw new DiyException("444", "courseId不允许为空");
        }

        return ChapterVoList;
    }

    //根据chapterId来删除 章节 有小节的章节不能删除
    @Override
    @Transactional
    public boolean deleteChapterByChapterId(String chapterId) {
        //1.先判断该章节下是否有小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("chapter_id", chapterId);
        List<EduVideo> videoList = eduVideoService.list(videoWrapper);
        boolean result = false ;
        if (videoList.size() <= 0) {
           result = this.removeById(chapterId);
        } else {
            throw new DiyException("400", "盖章节下存在小节");
        }
        return result;
    }

    //新增章节
    @Override
    @Transactional
    public String saveChapter(ChapterVo chapterVo) {

        if (chapterVo.getTitle() == null) {
            throw new DiyException("400", "eduVideo对象不允许为空");
        } else {
            EduChapter chapter = new EduChapter();
            BeanUtils.copyProperties(chapterVo, chapter);
            boolean save = this.save(chapter);

            String chapterId = save ? chapter.getId() : null;
            return chapterId;
        }
    }

    //更新章节
    @Override
    public boolean updateById(String chapterId, ChapterVo chapterVo) {

        boolean result = false ;
        if(StringUtils.isEmpty(chapterId) || StringUtils.isEmpty(chapterVo.getTitle()) ){
            throw new DiyException("400","章节标题和章节Id不能为空");
        }else {
            EduChapter chapter = new EduChapter();
            BeanUtils.copyProperties(chapterVo, chapter);
            chapter.setId(chapterId);
            result = this.updateById(chapter);
        }
        return result ;
    }

    //deleteByCourseId
    @Override
    public boolean removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        boolean result = this.remove(chapterQueryWrapper);
        return result;
    }
}




